package com.aismy.admin.controller;

import com.aismy.admin.dto.QuestionDTO;
import com.aismy.admin.entity.Question;
import com.aismy.admin.service.QuestionService;

import com.aismy.admin.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin/questions")
@RequiredArgsConstructor
public class QuestionController {

    private final QuestionService questionService;

    @GetMapping
    public Result<Page<Question>> page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer scaleId,
            @RequestParam(required = false) String query) {
        return Result.success(questionService.getQuestionPage(page, size,scaleId, query));
    }

    @GetMapping("/{id}")
    public Result<QuestionDTO> getById(@PathVariable Long id) {
        return Result.success(questionService.getQuestionWithOptions(id));
    }

    @PostMapping
    public Result<Void> add(@RequestBody QuestionDTO questionDTO) {
        questionService.saveQuestionWithOptions(questionDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody QuestionDTO questionDTO) {
        questionDTO.setId(id);
        questionService.updateQuestionWithOptions(questionDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        questionService.removeQuestionWithOptions(id);
        return Result.success();
    }
} 